#include "../include/alphadog_monitor/setting.h"
#include "ui_setting.h"
#include <QTabWidget>
#include <QSettings>
#include <QTreeWidget>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>

Setting::Setting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Setting)
{
    ui->setupUi(this);

    // 初始化控件
    InitWidget();

    // 隐藏
    this->hide();
}

Setting::~Setting()
{
    delete ui;
}

void Setting::InitWidget(void)
{
    // 返回
    connect(ui->btnReturn,&QPushButton::clicked, [=]() {
        this->close();
    });

    // 保存设置
    connect(ui->btnSaveSettings, &QPushButton::clicked, [=]() {
        WriteSettings();
    });

    // 绑定回车键，快速保存设置
    connect(ui->lineEdit_RobotModel, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_RobotName, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_JointsDriver, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_FeetDriver, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_FeetData, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_PoseData, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_InitImg, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_HandledImg, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_JoystickCmd, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
    connect(ui->lineEdit_StatusCmd, SIGNAL(returnPressed()), this, SLOT(On_ButtonClicked()));
}

void Setting::Display(int index)
{
    ui->tabWidget->setCurrentIndex(index);
    ReadSettings();
    this->show();
}

void Setting::ReadSettings(void)
{
    // 机器人
    QSettings main_setting("alphadog_monitor","settings");
    ui->lineEdit_RobotModel->setText(main_setting.value("robot/model", "/alphadog_monitor/robot_model").toString());
    ui->lineEdit_RobotName->setText(main_setting.value("robot/name", "robot_name").toString());

    // 驱动
    ui->lineEdit_JointsDriver->setText(main_setting.value("driver/joints_driver", "/alphadog_monitor/joints_driver").toString());
    ui->lineEdit_FeetDriver->setText(main_setting.value("driver/feet_driver", "/alphadog_monitor/feet_driver").toString());
    ui->lineEdit_FeetData->setText(main_setting.value("driver/feet_data", "/alphadog_monitor/feet_data").toString());
    ui->lineEdit_PoseData->setText(main_setting.value("driver/pose_data", "/alphadog_monitor/pose_data").toString());

    // 图像
    ui->lineEdit_InitImg->setText(main_setting.value("image/init_img","/alphadog_monitor/init_img").toString());
    ui->lineEdit_HandledImg->setText(main_setting.value("image/handled_img", "/alphadog_monitor/handled_img").toString());

    // 指令
    ui->lineEdit_JoystickCmd->setText(main_setting.value("command/joystick", "alphadog_monitor/joystick_cmd").toString());
    ui->lineEdit_StatusCmd->setText(main_setting.value("command/status", "alphadog_monitor/status_cmd").toString());
}

void Setting::WriteSettings(void)
{
    // 机器人
    QSettings main_setting("alphadog_monitor","settings");
    main_setting.setValue("robot/model", ui->lineEdit_RobotModel->text());
    main_setting.setValue("robot/name", ui->lineEdit_RobotName->text());

    // 驱动
    main_setting.setValue("driver/joints_driver", ui->lineEdit_JointsDriver->text());
    main_setting.setValue("driver/feet_driver", ui->lineEdit_FeetDriver->text());
    main_setting.setValue("driver/feet_data", ui->lineEdit_FeetData->text());
    main_setting.setValue("driver/pose_data", ui->lineEdit_PoseData->text());

    // 图像
    main_setting.setValue("image/init_img", ui->lineEdit_InitImg->text());
    main_setting.setValue("image/handled_img", ui->lineEdit_HandledImg->text());

    // 指令
    main_setting.setValue("command/joystick", ui->lineEdit_JoystickCmd->text());
    main_setting.setValue("command/status", ui->lineEdit_StatusCmd->text());

    // 提示信息
    QMessageBox::information(NULL, "成功信息",
                             "设置保存成功",
                             QMessageBox::Yes);
}

/**
 * @brief 回车保存设置
 */
void Setting::On_ButtonClicked(void)
{
    WriteSettings();
}
